# encoding=utf-8
import threading

import requests
import time
import traceback
from seg_system.application_config import ApplicationConfig


def connect_spring():
    """
        这是一个守护线程, 通过主动触发接口向Spring传递心跳连接
    """
    while True:
        dst_url = "http://{}:{}/app/scheduler/heart".format(ApplicationConfig.URLConfig.BASE_URL,
                                                            ApplicationConfig.URLConfig.BASE_PORT)
        try:
            conn = requests.get(dst_url)
            res = conn.json()
            if res['result'] != 0 or res['code'] != 200:
                # 说明spring boot连接异常
                time.sleep(0.5)
            else:
                time.sleep(5)  # 5s的心跳链接
        except:
            # 说明flask还没启动完成
            print(traceback.format_exc())
            time.sleep(0.5)


# spring通信守护线程, 直接在main里启动冲突
t = threading.Thread(target=connect_spring)
t.start()
print("hh")
